package com.wg.springboot.jdk.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description:
 * @Author WuGang
 * @Date 2021/10/25 10:28
 * @Version 1.0
 */
public class AtomicInt {
    public static void main(String[] args) {
//        线程安全的int 多个线程可以共享变量 效率较低 这种类一般都是调他的方法而不是普通加减
        AtomicInteger count = new AtomicInteger();
        System.out.println(count);  //0
        System.out.println(count.get()); //0
        count.set(999);
        System.out.println(count); //999
        int expected=999;
        int b=9;
        boolean b1 = count.compareAndSet(expected, b);  //前面的count达到expected预期值就把b赋值给count
        System.out.println(b1);  //t
        System.out.println(count);  //9
        System.out.println("////////////////");
        AtomicInteger count2 = new AtomicInteger(5);
        System.out.println(count2.getAndAdd(5));//5  现获取再加
        System.out.println(count2.get());//10 到这一一行就是已经加好了

        System.out.println(count2.addAndGet(10));//20
        System.out.println(count2.get());//20  这两个值就是一样的  不存在先获取后运算

        System.out.println(count2.getAndDecrement());//先获取再自减1    =20
        System.out.println(count2.get());//19
//        先自减后获取到减1后的值
        System.out.println(count2.decrementAndGet());//18
        System.out.println(count2.get());//18

    }
}
